import React from 'react';

import router from 'umi/router';
import moment from 'moment';

import { connect } from 'dva';

import { Button,Form, InputNumber, Select,DatePicker } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;


const pathAfterSubmit = '/AccountMove/AccountBalanceOpen'


@connect(({
  accountBalanceLine,accountAccount, resPartner, productProduct, accountAnalyticAccount,
  accountJournal
}) => ({
  accountBalanceLine,accountAccount, resPartner, productProduct, accountAnalyticAccount,
  accountJournal

}))
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id, balance_id } } } = this.props;

    this.id = Number(id)
    this.balance_id = Number(balance_id)

    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountBalanceLine/view',
        payload:  { id: this.id }
      });

    }

    // 获取多对一字段的下拉列表框选项所需要的数据

    const { dispatch } = this.props;
    await dispatch({
      type: 'accountAccount/search',
      payload: {domain:[], fields:{} },
    });

    await dispatch({
      type: 'resPartner/search',
      payload: {domain:[], fields:{} },
    });

    await dispatch({
      type: 'productProduct/search',
      payload: {domain:[], fields:{} },
    });

    await dispatch({
      type: 'accountAnalyticAccount/search',
      payload: {domain:[], fields:{} },
    });

    await dispatch({
      type: 'accountJournal/search',
      payload: {domain:[], fields:{} },
    });


  }

  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    const create_or_write = async (vals) => {

      if(this.id){
        await dispatch({
          type: 'accountBalanceLine/write',
          payload : {
            id:this.id,  vals,
          },
        });

        router.push(pathAfterSubmit);
      }
      else{

        await dispatch({
          type: 'accountBalanceLine/create',
          payload : {
            vals: { ...vals, balance_id: this.balance_id, type:'open' },
          },
        });

        router.push(pathAfterSubmit);

      }


    }

    form.validateFieldsAndScroll( async (err, values) => {
      if (!err) {

        const { date_maturity } = values;

        const vals = { ...values,
          date_maturity: date_maturity.format('YYYY-MM-DD'),
        };

        await create_or_write(vals)

      }
    });
  };


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { accountBalanceLine: { record } } = this.props;
    const { accountAccount: { recordsList: accountList } } = this.props;
    const { resPartner: { recordsList: partnerList } } = this.props;
    const { productProduct: { recordsList: productList } } = this.props;
    const { accountAnalyticAccount: { recordsList: analyticList } } = this.props;
    const { accountJournal: { recordsList: journalList } } = this.props;





    return (
      <div>
          <Form onSubmit={this.handleSubmit}>
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="科目">
              {getFieldDecorator('account_id', {
                rules: [{ required: true }],
                initialValue: record.account_id ? record.account_id.id : null
              })(
                <Select placeholder="科目" >
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="借方金额">
              {getFieldDecorator('debit', {
                rules: [{ required: true }],
                initialValue: record.debit || 0,
              })(
                <InputNumber />
              )}
            </FormItem>

            <FormItem label="贷方金额">
              {getFieldDecorator('credit', {
                rules: [{ required: true }],
                initialValue: record.credit || 0,
              })(
                <InputNumber />
              )}
            </FormItem>

            <FormItem label="业务伙伴">
              {getFieldDecorator('partner_id', {
                //rules: [{ required: true }],
                initialValue: record.partner_id ? record.partner_id.id : null
              })(
                <Select placeholder="业务伙伴" >
                  <Option value={0} >未选择</Option>
                  {partnerList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.ref} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="产品或服务">
              {getFieldDecorator('product_id', {
                //rules: [{ required: true }],
                initialValue: record.product_id ? record.product_id.id : null
              })(
                <Select placeholder="产品或服务" >
                  <Option value={0} >未选择</Option>
                  {productList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>


            <FormItem label="分析科目">
              {getFieldDecorator('analytic_account_id', {
                //rules: [{ required: true }],
                initialValue: record.analytic_account_id ? record.analytic_account_id.id : null
              })(
                <Select placeholder="分析科目" >
                  <Option value={0} >未选择</Option>
                  {analyticList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="银行存款账户">
              {getFieldDecorator('journal_id', {
                //rules: [{ required: true }],
                initialValue: record.journal_id ? record.journal_id.id : null
              })(
                <Select placeholder="银行存款账户" >
                  <Option value={0} >未选择</Option>
                  {journalList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="到期">
              {getFieldDecorator('date_maturity', {
                initialValue: record.date_maturity ? moment(record.date_maturity, 'YYYY-MM-DD') : moment()
              })(<DatePicker />)}
            </FormItem>


          </Form>

      </div>

    );
  }
}


export default Form.create()(FormCreate);


